package com.engine.Mangers;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;

import com.util.EngineUtil;
import com.wfe.db.SessionUtil;
import com.wfe.db.WorkFlowEngineQueryUtil;
import com.wfe.mapping.client.WfFirePlanAction;
import com.wfe.mapping.client.WfPlan;
import com.wfe.mapping.client.WfPlanTask;
import com.wfe.mapping.client.WfProject;
import com.wfe.mapping.client.WfProjectStatus;
import com.wfe.mapping.client.WfProjectTask;

/**
 * @author Kareem.Yehya
 * @since 30 March 2009 <br>
 *        * @version 1.0.0.1
 * 
 * 
 * <br>
 *        This Class Resposible for:- <br>
 * <br>
 *        1- Create project From Plan and action <br>
 *        2- Create Project Tasks <br>
 *        3- Change project Status. <br>
 *        4- End Project.
 * 
 * 
 */
public class ProjectManger
{

	/**
	 * @author kareem_yehya
	 * @since 02 April 2009 <br>
	 *        This method is the entry point for the project manger
	 * 
	 * 
	 * 
	 * */
	public void executeAction(WfPlan wfPlan, WfFirePlanAction action)
	{

		Session session = null;
		try
		{
			WfProject project = CreateProject(wfPlan, action);

			boolean iscreated = createProjectTasks(project, wfPlan, action);
			if(iscreated)
			{
				action.setIsProcessed(true);
				session = SessionUtil.getSession();
				WorkFlowEngineQueryUtil.updateWfFirePlanAction(action, session);

			}
		}catch(Exception e)
		{
			e.printStackTrace();

		}finally
		{
			EngineUtil.closeSession(session);
		}

	}

	private boolean createProjectTasks(WfProject project, WfPlan wfPlan, WfFirePlanAction action)
	{
		Session session = null;

		try
		{

			session = SessionUtil.getSession();
			List<WfPlanTask> taskList = WorkFlowEngineQueryUtil.getPlanTasks(session, wfPlan);
			if(taskList != null)
			{
				for(WfPlanTask planTask : taskList)
				{
					Long priority = action.getPriority();
					createWfProjectTask(planTask, project, wfPlan, priority, action);

				}// for end
				return true;
			}
			else
			{

				return false;
			}

		}catch(Exception e)
		{

			e.printStackTrace();
			return false;
		}finally
		{
			EngineUtil.closeSession(session);
		}

	}

	/**
	 * @author kareem_yehya
	 * @since 02 April 2009 <br>
	 *        This method create a project using wfPlan and action
	 * @return WfProject
	 * @param WfPlan
	 *            wfPlan <br>
	 *            plan suitable for the project
	 * @param WfFirePlanAction
	 *            action <coming action>
	 * @see com.wfe.mapping.client.WfProject
	 * @see com.wfe.mapping.client.WfPlan
	 * 
	 * 
	 * */
	public WfProject CreateProject(WfPlan wfPlan, WfFirePlanAction action)
	{

		Session session = null;
		try
		{
			WfProject project = new WfProject();
			project.setRecid(WorkFlowEngineQueryUtil.getRandomID());
			project.setStartTime(new Date());
			project.setWfPlan(wfPlan);
			project.setWfFirePlanAction(action);
			project.setWfProjectStatus(getWfProjectStatus(1l));
			project.setTitle(wfPlan.getName() + action.getActionTime() + action.getRecid());
			// Session session = SessionUtil.getCurrentSession();
			// create session
			session = SessionUtil.getSession();
			WorkFlowEngineQueryUtil.addWfProject(project, session);

			return project;
		}catch(Exception e)
		{

			e.printStackTrace();
			return null;
		}

		finally
		{
			EngineUtil.closeSession(session);
		}

	}

	/**
	 * @author kareem_yehya
	 * @since 04 April 2009 <br>
	 *        Call this method to return WfProjectStatus
	 * 
	 * @return WfProjectStatus
	 * */

	public WfProjectStatus getWfProjectStatus(long statusID)
	{
		Session session = null;
		try
		{
			session = SessionUtil.getSession();
			WfProjectStatus status = WorkFlowEngineQueryUtil.getwfProjectStatusByID(statusID, session);

			return status;
		}catch(Exception e)
		{

			e.printStackTrace();
		}finally
		{
			EngineUtil.closeSession(session);
		}
		return null;
	}

	/**
	 * @author kareem_yehya
	 * @param priority
	 * @since 04 April 2009 <br>
	 * <br>
	 *        add project task
	 * 
	 * @return
	 * */

	// @SuppressWarnings("deprecation")
	public WfProjectTask createWfProjectTask(WfPlanTask planTask, WfProject project, WfPlan wfPlan, Long priority, WfFirePlanAction action)
	{
		Session session = null;
		try
		{
			WfProjectTask projectTask = new WfProjectTask();
			projectTask.setRecid(WorkFlowEngineQueryUtil.getRandomID());
			projectTask.setCreationTime(action.getActionTime());// to path
			// action time
			projectTask.setWfPlanTask(planTask);
			projectTask.setPriority(priority);
			projectTask.setWfTask(planTask.getWfTask());

			// -----------------should be
			// lazy-------------------------------------

			projectTask.setWfTaskType(planTask.getWfTask().getWfTaskType());

			// -------------------------------------------------------

			projectTask.setWfProject(project);
			projectTask.setIsProcessed(false);
			// ------------------ created status ----------------------------
			// Session session = SessionUtil.getCurrentSession();
			session = SessionUtil.getSession();
			projectTask.setWfProjectTaskStatus(WorkFlowEngineQueryUtil.getWfProjectTaskStatusByID(5, session));

			// ------------------ add wf_task-----------------------------

			WorkFlowEngineQueryUtil.addProjectTask(session, projectTask);

			return null;
		}catch(Exception e)
		{

			e.printStackTrace();
		}finally
		{

			EngineUtil.closeSession(session);
		}
		return null;
	}

	public WfProjectTask createWfProjectTask(WfPlanTask planTask, WfProject project, WfPlan wfPlan, Long priority)
	{
		Session session = null;
		try
		{
			WfProjectTask projectTask = new WfProjectTask();
			projectTask.setRecid(WorkFlowEngineQueryUtil.getRandomID());
			projectTask.setCreationTime(new Date());
			projectTask.setWfPlanTask(planTask);
			projectTask.setPriority(priority);
			projectTask.setWfTask(planTask.getWfTask());

			// -----------------should be
			// lazy-------------------------------------

			projectTask.setWfTaskType(planTask.getWfTask().getWfTaskType());

			// -------------------------------------------------------

			projectTask.setWfProject(project);
			projectTask.setIsProcessed(false);
			// ------------------ created status ----------------------------

			session = SessionUtil.getSession();
			projectTask.setWfProjectTaskStatus(WorkFlowEngineQueryUtil.getWfProjectTaskStatusByID(5, session));

			// ------------------ add wf_task-----------------------------

			WorkFlowEngineQueryUtil.addProjectTask(session, projectTask);

			return null;
		}catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			EngineUtil.closeSession(session);
		}
		return null;
	}

}
